package module03.task05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class DouDiZhuTest {
    public static void main(String[] args) {
        // 创建List集合，键是编号，值是牌
        HashMap<Integer, String> pokers = new HashMap<Integer, String>();
        // 创建List集合，存储编号
        ArrayList<Integer> pokerNumber = new ArrayList<Integer>();
        // 定义出13个点数的数组
        String[] numbers = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };
        String[] colors = { "黑桃", "红心", "梅花", "方片" };
        // 定义整数变量，作为键的出现
        int index = 2;
        // 遍历数组，花色+点数的组合，存储到Map集合
        for (String number : numbers) {
            for (String color : colors) {
                pokerNumber.add(index);
                pokers.put(index++, color + number);
            }
        }
        pokers.put(0, "大王");
        pokers.put(1, "小王");
        pokerNumber.add(0);
        pokerNumber.add(1);
        System.out.println("组合牌:\n"+pokers);
//		System.out.println(pokerNumber);

        // 洗牌
        Collections.shuffle(pokerNumber);
        System.out.println("洗牌结果：\n"+pokerNumber);

        // 发牌，将牌的编号，发给玩家集合，底牌集合，四个集合
        ArrayList<Integer> player1 = new ArrayList<Integer>();
        ArrayList<Integer> player2 = new ArrayList<Integer>();
        ArrayList<Integer> player3 = new ArrayList<Integer>();
        ArrayList<Integer> bottom = new ArrayList<Integer>();

        // 发牌采用的是索引%玩家数
        for (int i = 0; i < pokerNumber.size(); i++) {
            if (i < 3) {
                bottom.add(pokerNumber.get(i));
            } else if (i % 3 == 0) {
                player1.add(pokerNumber.get(i));
            } else if (i % 3 == 1) {
                player2.add(pokerNumber.get(i));
            } else if (i % 3 == 2) {
                player3.add(pokerNumber.get(i));
            }
        }

        // 对玩家手中的编号排序
        System.out.println();
        System.out.println("发牌结果：");
        look("路人甲：",player1,pokers);
        look("路人乙：",player2,pokers);
        look("路人丙：",player3,pokers);
        look("底牌：",bottom,pokers);

        System.out.println();
        System.out.println("每个人的牌排序：");
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(bottom);

        // 看牌，根据玩家手中的编号，到Map集合中查找，根据键找值
        look("路人甲：",player1,pokers);
        look("路人乙：",player2,pokers);
        look("路人丙：",player3,pokers);
        look("底牌：",bottom,pokers);

    }

    // 看牌
    public static void look(String name,ArrayList<Integer> player, HashMap<Integer, String> pooker) {
        System.out.print(name+" ");
        for (int i = 0; i < player.size(); i++) {
            Integer key = player.get(i);
            String value = pooker.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }
}
